home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / System / Common / GC / h / gc
Text File  |  1996-11-11  |  29KB  |  648 lines

  1. /* 
  2.  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3.  * Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved.
  4.  * Copyright 1996 by Silicon Graphics.  All rights reserved.
  5.  *
  6.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  7.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  8.  *
  9.  * Permission is hereby granted to use or copy this program
  10.  * for any purpose,  provided the above notices are retained on all copies.
  11.  * Permission to modify the code and to distribute modified code is granted,
  12.  * provided the above notices are retained, and a notice that the code was
  13.  * modified is included with the above copyright notice.
  14.  */
  15.  
  16. /*
  17.  * Note that this defines a large number of tuning hooks, which can
  18.  * safely be ignored in nearly all cases.  For normal use it suffices
  19.  * to call only GC_MALLOC and perhaps GC_REALLOC.
  20.  * For better performance, also look at GC_MALLOC_ATOMIC, and
  21.  * GC_enable_incremental.  If you need an action to be performed
  22.  * immediately before an object is collected, look at GC_register_finalizer.
  23.  * If you are using Solaris threads, look at the end of this file.
  24.  * Everything else is best ignored unless you encounter performance
  25.  * problems.
  26.  */
  27.  
  28. #ifndef _GC_H
  29.  
  30. # define _GC_H
  31. # define __GC
  32. # include <stddef.h>
  33.  
  34.  
  35. #if defined(_MSC_VER) && defined(_DLL)
  36. #ifdef GC_BUILD
  37. #define GC_API __declspec(dllexport)
  38. #else
  39. #define GC_API __declspec(dllimport)
  40. #endif
  41. #endif
  42.  
  43. #ifndef GC_API
  44. #define GC_API extern
  45. #endif
  46.  
  47. # if defined(__STDC__) || defined(__cplusplus)
  48. #   define GC_PROTO(args) args
  49.     typedef void * GC_PTR;
  50. # else
  51. #   define GC_PROTO(args) ()
  52.     typedef char * GC_PTR;
  53. #  endif
  54.  
  55. # ifdef __cplusplus
  56.     extern "C" {
  57. # endif
  58.  
  59.  
  60. /* Define word and signed_word to be unsigned and signed types of the     */
  61. /* size as char * or void *.  There seems to be no way to do this    */
  62. /* even semi-portably.  The following is probably no better/worse     */
  63. /* than almost anything else.                        */
  64. /* The ANSI standard suggests that size_t and ptr_diff_t might be     */
  65. /* better choices.  But those appear to have incorrect definitions    */
  66. /* on may systems.  Notably "typedef int size_t" seems to be both    */
  67. /* frequent and WRONG.                            */
  68. typedef unsigned long GC_word;
  69. typedef long GC_signed_word;
  70.  
  71. /* Public read-only variables */
  72.  
  73. GC_API GC_word GC_gc_no;/* Counter incremented per collection.      */
  74.             /* Includes empty GCs at startup.        */
  75.             
  76.  
  77. /* Public R/W variables */
  78.  
  79. GC_API void * (*GC_oom_fn) GC_PROTO((size_t bytes_requested));
  80.             /* When there is insufficient memory to satisfy */
  81.             /* an allocation request, we return        */
  82.             /* (*GC_oom_fn)().  By default this just    */
  83.             /* returns 0.                    */
  84.             /* If it returns, it must return 0 or a valid    */
  85.             /* pointer to a previously allocated heap     */
  86.             /* object.                    */
  87.  
  88. GC_API int GC_quiet;    /* Disable statistics output.  Only matters if    */
  89.             /* collector has been compiled with statistics    */
  90.             /* enabled.  This involves a performance cost,    */
  91.             /* and is thus not the default.            */
  92.  
  93. GC_API int GC_dont_gc;    /* Dont collect unless explicitly requested, e.g. */
  94.             /* because it's not safe.              */
  95.  
  96. GC_API int GC_dont_expand;
  97.             /* Dont expand heap unless explicitly requested */
  98.             /* or forced to.                */
  99.  
  100. GC_API int GC_full_freq;    /* Number of partial collections between    */
  101.                 /* full collections.  Matters only if    */
  102.                 /* GC_incremental is set.            */
  103.             
  104. GC_API GC_word GC_non_gc_bytes;
  105.             /* Bytes not considered candidates for collection. */
  106.             /* Used only to control scheduling of collections. */
  107.  
  108. GC_API GC_word GC_free_space_divisor;
  109.             /* We try to make sure that we allocate at     */
  110.             /* least N/GC_free_space_divisor bytes between    */
  111.             /* collections, where N is the heap size plus    */
  112.             /* a rough estimate of the root set size.    */
  113.             /* Initially, GC_free_space_divisor = 4.    */
  114.             /* Increasing its value will use less space    */
  115.             /* but more collection time.  Decreasing it    */
  116.             /* will appreciably decrease collection time    */
  117.             /* at the expense of space.            */
  118.             /* GC_free_space_divisor = 1 will effectively    */
  119.             /* disable collections.                */
  120.  
  121. GC_API GC_word GC_max_retries;
  122.             /* The maximum number of GCs attempted before    */
  123.             /* reporting out of memory after heap        */
  124.             /* expansion fails.  Initially 0.        */
  125.             
  126.             
  127. /* Public procedures */
  128. /*
  129.  * general purpose allocation routines, with roughly malloc calling conv.
  130.  * The atomic versions promise that no relevant pointers are contained
  131.  * in the object.  The nonatomic versions guarantee that the new object
  132.  * is cleared.  GC_malloc_stubborn promises that no changes to the object
  133.  * will occur after GC_end_stubborn_change has been called on the
  134.  * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
  135.  * that is scanned for pointers to collectable objects, but is not itself
  136.  * collectable.  GC_malloc_uncollectable and GC_free called on the resulting
  137.  * object implicitly update GC_non_gc_bytes appropriately.
  138.  */
  139. GC_API GC_PTR GC_malloc GC_PROTO((size_t size_in_bytes));
  140. GC_API GC_PTR GC_malloc_atomic GC_PROTO((size_t size_in_bytes));
  141. GC_API GC_PTR GC_malloc_uncollectable GC_PROTO((size_t size_in_bytes));
  142. GC_API GC_PTR GC_malloc_stubborn GC_PROTO((size_t size_in_bytes));
  143.  
  144. /* The following is only defined if the library has been suitably    */
  145. /* compiled:                                */
  146. GC_API GC_PTR GC_malloc_atomic_uncollectable GC_PROTO((size_t size_in_bytes));
  147.  
  148. /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
  149. /* Requires a pointer to the base of an object.                */
  150. /* If the argument is stubborn, it should not be changeable when freed. */
  151. /* An object should not be enable for finalization when it is         */
  152. /* explicitly deallocated.                        */
  153. /* GC_free(0) is a no-op, as required by ANSI C for free.        */
  154. GC_API void GC_free GC_PROTO((GC_PTR object_addr));
  155.  
  156. /*
  157.  * Stubborn objects may be changed only if the collector is explicitly informed.
  158.  * The collector is implicitly informed of coming change when such
  159.  * an object is first allocated.  The following routines inform the
  160.  * collector that an object will no longer be changed, or that it will
  161.  * once again be changed.  Only nonNIL pointer stores into the object
  162.  * are considered to be changes.  The argument to GC_end_stubborn_change
  163.  * must be exacly the value returned by GC_malloc_stubborn or passed to
  164.  * GC_change_stubborn.  (In the second case it may be an interior pointer
  165.  * within 512 bytes of the beginning of the objects.)
  166.  * There is a performance penalty for allowing more than
  167.  * one stubborn object to be changed at once, but it is acceptable to
  168.  * do so.  The same applies to dropping stubborn objects that are still
  169.  * changeable.
  170.  */
  171. GC_API void GC_change_stubborn GC_PROTO((GC_PTR));
  172. GC_API void GC_end_stubborn_change GC_PROTO((GC_PTR));
  173.  
  174. /* Return a pointer to the base (lowest address) of an object given    */
  175. /* a pointer to a location within the object.                */
  176. /* Return 0 if displaced_pointer doesn't point to within a valid    */
  177. /* object.                                */
  178. GC_API GC_PTR GC_base GC_PROTO((GC_PTR displaced_pointer));
  179.  
  180. /* Given a pointer to the base of an object, return its size in bytes.    */
  181. /* The returned size may be slightly larger than what was originally    */
  182. /* requested.                                */
  183. GC_API size_t GC_size GC_PROTO((GC_PTR object_addr));
  184.  
  185. /* For compatibility with C library.  This is occasionally faster than    */
  186. /* a malloc followed by a bcopy.  But if you rely on that, either here    */
  187. /* or with the standard C library, your code is broken.  In my        */
  188. /* opinion, it shouldn't have been invented, but now we're stuck. -HB    */
  189. /* The resulting object has the same kind as the original.        */
  190. /* If the argument is stubborn, the result will have changes enabled.    */
  191. /* It is an error to have changes enabled for the original object.    */
  192. /* Follows ANSI comventions for NULL old_object.            */
  193. GC_API GC_PTR GC_realloc GC_PROTO((GC_PTR old_object,
  194.                    size_t new_size_in_bytes));
  195.                    
  196. /* Explicitly increase the heap size.    */
  197. /* Returns 0 on failure, 1 on success.  */
  198. GC_API int GC_expand_hp GC_PROTO((size_t number_of_bytes));
  199.  
  200. /* Limit the heap size to n bytes.  Useful when you're debugging,     */
  201. /* especially on systems that don't handle running out of memory well.    */
  202. /* n == 0 ==> unbounded.  This is the default.                */
  203. GC_API void GC_set_max_heap_size GC_PROTO((GC_word n));
  204.  
  205. /* Clear the set of root segments.  Wizards only. */
  206. GC_API void GC_clear_roots GC_PROTO((void));
  207.  
  208. /* Add a root segment.  Wizards only. */
  209. GC_API void GC_add_roots GC_PROTO((char * low_address,
  210.                    char * high_address_plus_1));
  211.  
  212. /* Add a displacement to the set of those considered valid by the    */
  213. /* collector.  GC_register_displacement(n) means that if p was returned */
  214. /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
  215. /* pointer to n.  N must be small and less than the size of p.        */
  216. /* (All pointers to the interior of objects from the stack are        */
  217. /* considered valid in any case.  This applies to heap objects and    */
  218. /* static data.)                            */
  219. /* Preferably, this should be called before any other GC procedures.    */
  220. /* Calling it later adds to the probability of excess memory        */
  221. /* retention.                                */
  222. /* This is a no-op if the collector was compiled with recognition of    */
  223. /* arbitrary interior pointers enabled, which is now the default.    */
  224. GC_API void GC_register_displacement GC_PROTO((GC_word n));
  225.  
  226. /* The following version should be used if any debugging allocation is    */
  227. /* being done.                                */
  228. GC_API void GC_debug_register_displacement GC_PROTO((GC_word n));
  229.  
  230. /* Explicitly trigger a full, world-stop collection.     */
  231. GC_API void GC_gcollect GC_PROTO((void));
  232.  
  233. /* Trigger a full world-stopped collection.  Abort the collection if     */
  234. /* and when stop_func returns a nonzero value.  Stop_func will be     */
  235. /* called frequently, and should be reasonably fast.  This works even    */
  236. /* if virtual dirty bits, and hence incremental collection is not     */
  237. /* available for this architecture.  Collections can be aborted faster    */
  238. /* than normal pause times for incremental collection.  However,    */
  239. /* aborted collections do no useful work; the next collection needs    */
  240. /* to start from the beginning.                        */
  241. typedef int (* GC_stop_func) GC_PROTO((void));
  242. GC_API int GC_try_to_collect GC_PROTO((GC_stop_func stop_func));
  243.  
  244. /* Return the number of bytes in the heap.  Excludes collector private    */
  245. /* data structures.  Includes empty blocks and fragmentation loss.    */
  246. /* Includes some pages that were allocated but never written.        */
  247. GC_API size_t GC_get_heap_size GC_PROTO((void));
  248.  
  249. /* Return the number of bytes allocated since the last collection.    */
  250. GC_API size_t GC_get_bytes_since_gc GC_PROTO((void));
  251.  
  252. /* Enable incremental/generational collection.    */
  253. /* Not advisable unless dirty bits are         */
  254. /* available or most heap objects are        */
  255. /* pointerfree(atomic) or immutable.        */
  256. /* Don't use in leak finding mode.        */
  257. /* Ignored if GC_dont_gc is true.        */
  258. GC_API void GC_enable_incremental GC_PROTO((void));
  259.  
  260. /* Perform some garbage collection work, if appropriate.    */
  261. /* Return 0 if there is no more work to be done.        */
  262. /* Typically performs an amount of work corresponding roughly    */
  263. /* to marking from one page.  May do more work if further    */
  264. /* progress requires it, e.g. if incremental collection is    */
  265. /* disabled.  It is reasonable to call this in a wait loop    */
  266. /* until it returns 0.                        */
  267. GC_API int GC_collect_a_little GC_PROTO((void));
  268.  
  269. /* Allocate an object of size lb bytes.  The client guarantees that    */
  270. /* as long as the object is live, it will be referenced by a pointer    */
  271. /* that points to somewhere within the first 256 bytes of the object.    */
  272. /* (This should normally be declared volatile to prevent the compiler    */
  273. /* from invalidating this assertion.)  This routine is only useful    */
  274. /* if a large array is being allocated.  It reduces the chance of     */
  275. /* accidentally retaining such an array as a result of scanning an    */
  276. /* integer that happens to be an address inside the array.  (Actually,    */
  277. /* it reduces the chance of the allocator not finding space for such    */
  278. /* an array, since it will try hard to avoid introducing such a false    */
  279. /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
  280. /* for arrays likely to be larger than 100K or so.  For other systems,    */
  281. /* or if the collector is not configured to recognize all interior    */
  282. /* pointers, the threshold is normally much higher.            */
  283. GC_API GC_PTR GC_malloc_ignore_off_page GC_PROTO((size_t lb));
  284. GC_API GC_PTR GC_malloc_atomic_ignore_off_page GC_PROTO((size_t lb));
  285.  
  286. /* Debugging (annotated) allocation.  GC_gcollect will check         */
  287. /* objects allocated in this way for overwrites, etc.            */
  288. GC_API GC_PTR GC_debug_malloc
  289.     GC_PROTO((size_t size_in_bytes, char * descr_string, int descr_int));
  290. GC_API GC_PTR GC_debug_malloc_atomic
  291.     GC_PROTO((size_t size_in_bytes, char * descr_string, int descr_int));
  292. GC_API GC_PTR GC_debug_malloc_uncollectable
  293.     GC_PROTO((size_t size_in_bytes, char * descr_string, int descr_int));
  294. GC_API GC_PTR GC_debug_malloc_stubborn
  295.     GC_PROTO((size_t size_in_bytes, char * descr_string, int descr_int));
  296. GC_API void GC_debug_free GC_PROTO((GC_PTR object_addr));
  297. GC_API GC_PTR GC_debug_realloc
  298.     GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes,
  299.             char * descr_string, int descr_int));
  300.                     
  301. GC_API void GC_debug_change_stubborn GC_PROTO((GC_PTR));
  302. GC_API void GC_debug_end_stubborn_change GC_PROTO((GC_PTR));
  303. # ifdef GC_DEBUG
  304. #   define GC_MALLOC(sz) GC_debug_malloc(sz, __FILE__, __LINE__)
  305. #   define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, __FILE__, __LINE__)
  306. #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_debug_malloc_uncollectable(sz, \
  307.                             __FILE__, __LINE__)
  308. #   define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, __FILE__, \
  309.                                    __LINE__)
  310. #   define GC_FREE(p) GC_debug_free(p)
  311. #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  312.     GC_debug_register_finalizer(p, f, d, of, od)
  313. #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  314.     GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
  315. #   define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, __FILE__, \
  316.                                    __LINE__)
  317. #   define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
  318. #   define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
  319. #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  320.     GC_general_register_disappearing_link(link, GC_base(obj))
  321. #   define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
  322. # else
  323. #   define GC_MALLOC(sz) GC_malloc(sz)
  324. #   define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
  325. #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
  326. #   define GC_REALLOC(old, sz) GC_realloc(old, sz)
  327. #   define GC_FREE(p) GC_free(p)
  328. #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  329.     GC_register_finalizer(p, f, d, of, od)
  330. #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  331.     GC_register_finalizer_ignore_self(p, f, d, of, od)
  332. #   define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
  333. #   define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
  334. #   define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
  335. #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  336.     GC_general_register_disappearing_link(link, obj)
  337. #   define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
  338. # endif
  339. /* The following are included because they are often convenient, and    */
  340. /* reduce the chance for a misspecifed size argument.  But calls may    */
  341. /* expand to something syntactically incorrect if t is a complicated    */
  342. /* type expression.                              */
  343. # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
  344. # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
  345. # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
  346. # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
  347.  
  348. /* Finalization.  Some of these primitives are grossly unsafe.        */
  349. /* The idea is to make them both cheap, and sufficient to build        */
  350. /* a safer layer, closer to PCedar finalization.            */
  351. /* The interface represents my conclusions from a long discussion    */
  352. /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,         */
  353. /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and        */
  354. /* probably nobody else agrees with it.        Hans-J. Boehm  3/13/92    */
  355. typedef void (*GC_finalization_proc)
  356.       GC_PROTO((GC_PTR obj, GC_PTR client_data));
  357.  
  358. GC_API void GC_register_finalizer
  359.         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
  360.           GC_finalization_proc *ofn, GC_PTR *ocd));
  361. GC_API void GC_debug_register_finalizer
  362.         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
  363.           GC_finalization_proc *ofn, GC_PTR *ocd));
  364.     /* When obj is no longer accessible, invoke        */
  365.     /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
  366.     /* a points to b (after disappearing links have been    */
  367.     /* made to disappear), then only a will be        */
  368.     /* finalized.  (If this does not create any new        */
  369.     /* pointers to b, then b will be finalized after the    */
  370.     /* next collection.)  Any finalizable object that    */
  371.     /* is reachable from itself by following one or more    */
  372.     /* pointers will not be finalized (or collected).    */
  373.     /* Thus cycles involving finalizable objects should    */
  374.     /* be avoided, or broken by disappearing links.        */
  375.     /* All but the last finalizer registered for an object  */
  376.     /* is ignored.                        */
  377.     /* Finalization may be removed by passing 0 as fn.    */
  378.     /* Finalizers are implicitly unregistered just before   */
  379.     /* they are invoked.                    */
  380.     /* The old finalizer and client data are stored in    */
  381.     /* *ofn and *ocd.                    */ 
  382.     /* Fn is never invoked on an accessible object,        */
  383.     /* provided hidden pointers are converted to real     */
  384.     /* pointers only if the allocation lock is held, and    */
  385.     /* such conversions are not performed by finalization    */
  386.     /* routines.                        */
  387.     /* If GC_register_finalizer is aborted as a result of    */
  388.     /* a signal, the object may be left with no        */
  389.     /* finalization, even if neither the old nor new    */
  390.     /* finalizer were NULL.                    */
  391.     /* Obj should be the nonNULL starting address of an     */
  392.     /* object allocated by GC_malloc or friends.        */
  393.     /* Note that any garbage collectable object referenced    */
  394.     /* by cd will be considered accessible until the    */
  395.     /* finalizer is invoked.                */
  396.  
  397. /* Another versions of the above follow.  It ignores        */
  398. /* self-cycles, i.e. pointers from a finalizable object to    */
  399. /* itself.  There is a stylistic argument that this is wrong,    */
  400. /* but it's unavoidable for C++, since the compiler may        */
  401. /* silently introduce these.  It's also benign in that specific    */
  402. /* case.                            */
  403. GC_API void GC_register_finalizer_ignore_self
  404.     GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
  405.           GC_finalization_proc *ofn, GC_PTR *ocd));
  406. GC_API void GC_debug_register_finalizer_ignore_self
  407.     GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
  408.           GC_finalization_proc *ofn, GC_PTR *ocd));
  409.  
  410. /* The following routine may be used to break cycles between    */
  411. /* finalizable objects, thus causing cyclic finalizable        */
  412. /* objects to be finalized in the correct order.  Standard    */
  413. /* use involves calling GC_register_disappearing_link(&p),    */
  414. /* where p is a pointer that is not followed by finalization    */
  415. /* code, and should not be considered in determining         */
  416. /* finalization order.                        */
  417. GC_API int GC_register_disappearing_link GC_PROTO((GC_PTR * /* link */));
  418.     /* Link should point to a field of a heap allocated     */
  419.     /* object obj.  *link will be cleared when obj is    */
  420.     /* found to be inaccessible.  This happens BEFORE any    */
  421.     /* finalization code is invoked, and BEFORE any        */
  422.     /* decisions about finalization order are made.        */
  423.     /* This is useful in telling the finalizer that     */
  424.     /* some pointers are not essential for proper        */
  425.     /* finalization.  This may avoid finalization cycles.    */
  426.     /* Note that obj may be resurrected by another        */
  427.     /* finalizer, and thus the clearing of *link may    */
  428.     /* be visible to non-finalization code.          */
  429.     /* There's an argument that an arbitrary action should  */
  430.     /* be allowed here, instead of just clearing a pointer. */
  431.     /* But this causes problems if that action alters, or     */
  432.     /* examines connectivity.                */
  433.     /* Returns 1 if link was already registered, 0        */
  434.     /* otherwise.                        */
  435.     /* Only exists for backward compatibility.  See below:    */
  436.     
  437. GC_API int GC_general_register_disappearing_link
  438.     GC_PROTO((GC_PTR * /* link */, GC_PTR obj));
  439.     /* A slight generalization of the above. *link is    */
  440.     /* cleared when obj first becomes inaccessible.  This    */
  441.     /* can be used to implement weak pointers easily and    */
  442.     /* safely. Typically link will point to a location    */
  443.     /* holding a disguised pointer to obj.  (A pointer     */
  444.     /* inside an "atomic" object is effectively          */
  445.     /* disguised.)   In this way soft            */
  446.     /* pointers are broken before any object        */
  447.     /* reachable from them are finalized.  Each link    */
  448.     /* May be registered only once, i.e. with one obj    */
  449.     /* value.  This was added after a long email discussion */
  450.     /* with John Ellis.                    */
  451.     /* Obj must be a pointer to the first word of an object */
  452.     /* we allocated.  It is unsafe to explicitly deallocate */
  453.     /* the object containing link.  Explicitly deallocating */
  454.     /* obj may or may not cause link to eventually be    */
  455.     /* cleared.                        */
  456. GC_API int GC_unregister_disappearing_link GC_PROTO((GC_PTR * /* link */));
  457.     /* Returns 0 if link was not actually registered.    */
  458.     /* Undoes a registration by either of the above two    */
  459.     /* routines.                        */
  460.  
  461. /* Auxiliary fns to make finalization work correctly with displaced    */
  462. /* pointers introduced by the debugging allocators.            */
  463. GC_API GC_PTR GC_make_closure GC_PROTO((GC_finalization_proc fn, GC_PTR data));
  464. GC_API void GC_debug_invoke_finalizer GC_PROTO((GC_PTR obj, GC_PTR data));
  465.  
  466. /* GC_set_warn_proc can be used to redirect or filter warning messages.    */
  467. /* p may not be a NULL pointer.                        */
  468. typedef void (*GC_warn_proc) GC_PROTO((char *msg, GC_word arg));
  469. GC_API GC_warn_proc GC_set_warn_proc GC_PROTO((GC_warn_proc p));
  470.     /* Returns old warning procedure.    */
  471.     
  472. /* The following is intended to be used by a higher level    */
  473. /* (e.g. cedar-like) finalization facility.  It is expected    */
  474. /* that finalization code will arrange for hidden pointers to    */
  475. /* disappear.  Otherwise objects can be accessed after they    */
  476. /* have been collected.                        */
  477. /* Note that putting pointers in atomic objects or in         */
  478. /* nonpointer slots of "typed" objects is equivalent to     */
  479. /* disguising them in this way, and may have other advantages.    */
  480. # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
  481.     typedef GC_word GC_hidden_pointer;
  482. #   define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
  483. #   define REVEAL_POINTER(p) ((GC_PTR)(HIDE_POINTER(p)))
  484.     /* Converting a hidden pointer to a real pointer requires verifying    */
  485.     /* that the object still exists.  This involves acquiring the      */
  486.     /* allocator lock to avoid a race with the collector.        */
  487. # endif /* I_HIDE_POINTERS */
  488.  
  489. typedef GC_PTR (*GC_fn_type) GC_PROTO((GC_PTR client_data));
  490. GC_API GC_PTR GC_call_with_alloc_lock
  491.             GC_PROTO((GC_fn_type fn, GC_PTR client_data));
  492.  
  493. /* Check that p and q point to the same object.          */
  494. /* Fail conspicuously if they don't.                */
  495. /* Returns the first argument.                  */
  496. /* Succeeds if neither p nor q points to the heap.        */
  497. /* May succeed if both p and q point to between heap objects.    */
  498. GC_API GC_PTR GC_same_obj GC_PROTO((GC_PTR p, GC_PTR q));
  499.  
  500. /* Checked pointer pre- and post- increment operations.  Note that    */
  501. /* the second argument is in units of bytes, not multiples of the    */
  502. /* object size.  This should either be invoked from a macro, or the    */
  503. /* call should be automatically generated.                */
  504. GC_API GC_PTR GC_pre_incr GC_PROTO((GC_PTR *p, size_t how_much));
  505. GC_API GC_PTR GC_post_incr GC_PROTO((GC_PTR *p, size_t how_much));
  506.  
  507. /* Check that p is visible                        */
  508. /* to the collector as a possibly pointer containing location.        */
  509. /* If it isn't fail conspicuously.                    */
  510. /* Returns the argument in all cases.  May erroneously succeed        */
  511. /* in hard cases.  (This is intended for debugging use with        */
  512. /* untyped allocations.  The idea is that it should be possible, though    */
  513. /* slow, to add such a call to all indirect pointer stores.)        */
  514. /* Currently useless for multithreaded worlds.                */
  515. GC_API GC_PTR GC_is_visible GC_PROTO((GC_PTR p));
  516.  
  517. /* Check that if p is a pointer to a heap page, then it points to    */
  518. /* a valid displacement within a heap object.                */
  519. /* Fail conspicuously if this property does not hold.            */
  520. /* Uninteresting with ALL_INTERIOR_POINTERS.                */
  521. /* Always returns its argument.                        */
  522. GC_API GC_PTR GC_is_valid_displacement GC_PROTO((GC_PTR    p));
  523.  
  524. /* Safer, but slow, pointer addition.  Probably useful mainly with     */
  525. /* a preprocessor.  Useful only for heap pointers.            */
  526. #ifdef GC_DEBUG
  527. #   define GC_PTR_ADD3(x, n, type_of_result) \
  528.     ((type_of_result)GC_same_obj((x)+(n), (x)))
  529. #   define GC_PRE_INCR3(x, n, type_of_result) \
  530.     ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
  531. #   define GC_POST_INCR2(x, type_of_result) \
  532.     ((type_of_result)GC_post_incr(&(x), sizeof(*x))
  533. #   ifdef __GNUC__
  534. #       define GC_PTR_ADD(x, n) \
  535.         GC_PTR_ADD3(x, n, typeof(x))
  536. #   define GC_PRE_INCR(x, n) \
  537.         GC_PRE_INCR3(x, n, typeof(x))
  538. #   define GC_POST_INCR(x, n) \
  539.         GC_POST_INCR3(x, typeof(x))
  540. #   else
  541.     /* We can't do this right without typeof, which ANSI    */
  542.     /* decided was not sufficiently useful.  Repeatedly    */
  543.     /* mentioning the arguments seems too dangerous to be    */
  544.     /* useful.  So does not casting the result.        */
  545. #       define GC_PTR_ADD(x, n) ((x)+(n))
  546. #   endif
  547. #else    /* !GC_DEBUG */
  548. #   define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
  549. #   define GC_PTR_ADD(x, n) ((x)+(n))
  550. #   define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
  551. #   define GC_PRE_INCR(x, n) ((x) += (n))
  552. #   define GC_POST_INCR2(x, n, type_of_result) ((x)++)
  553. #   define GC_POST_INCR(x, n) ((x)++)
  554. #endif
  555.  
  556. /* Safer assignment of a pointer to a nonstack location.    */
  557. #ifdef GC_DEBUG
  558. # ifdef __STDC__
  559. #   define GC_PTR_STORE(p, q) \
  560.     (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
  561. # else
  562. #   define GC_PTR_STORE(p, q) \
  563.     (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
  564. # endif
  565. #else /* !GC_DEBUG */
  566. #   define GC_PTR_STORE(p, q) *((p) = (q))
  567. #endif
  568.  
  569. /* Fynctions called to report pointer checking errors */
  570. GC_API void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR p, GC_PTR q));
  571.  
  572. GC_API void (*GC_is_valid_displacement_print_proc)
  573.     GC_PROTO((GC_PTR p));
  574.  
  575. GC_API void (*GC_is_visible_print_proc)
  576.     GC_PROTO((GC_PTR p));
  577.  
  578. #ifdef SOLARIS_THREADS
  579. /* We need to intercept calls to many of the threads primitives, so     */
  580. /* that we can locate thread stacks and stop the world.            */
  581. /* Note also that the collector cannot see thread specific data.    */
  582. /* Thread specific data should generally consist of pointers to        */
  583. /* uncollectable objects, which are deallocated using the destructor    */
  584. /* facility in thr_keycreate.                        */
  585. # include <thread.h>
  586. # include <signal.h>
  587.   int GC_thr_create(void *stack_base, size_t stack_size,
  588.                     void *(*start_routine)(void *), void *arg, long flags,
  589.                     thread_t *new_thread);
  590.   int GC_thr_join(thread_t wait_for, thread_t *departed, void **status);
  591.   int GC_thr_suspend(thread_t target_thread);
  592.   int GC_thr_continue(thread_t target_thread);
  593.   void * GC_dlopen(const char *path, int mode);
  594.  
  595. # define thr_create GC_thr_create
  596. # define thr_join GC_thr_join
  597. # define thr_suspend GC_thr_suspend
  598. # define thr_continue GC_thr_continue
  599. # define dlopen GC_dlopen
  600.  
  601. # endif /* SOLARIS_THREADS */
  602.  
  603. #ifdef IRIX_THREADS
  604. /* We treat these similarly. */
  605. # include <pthread.h>
  606. # include <signal.h>
  607.  
  608.   int GC_pthread_create(pthread_t *new_thread,
  609.                         const pthread_attr_t *attr,
  610.                 void *(*start_routine)(void *), void *arg);
  611.   int GC_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
  612.   int GC_pthread_join(pthread_t thread, void **retval);
  613.  
  614. # define pthread_create GC_pthread_create
  615. # define pthread_sigmask GC_pthread_sigmask
  616. # define pthread_join GC_pthread_join
  617.  
  618. #endif /* IRIX_THREADS */
  619.  
  620. #if defined(SOLARIS_THREADS) || defined(IRIX_THREADS)
  621. /* This returns a list of objects, linked through their first        */
  622. /* word.  Its use can greatly reduce lock contention problems, since    */
  623. /* the allocation lock can be acquired and released many fewer times.    */
  624. GC_PTR GC_malloc_many(size_t lb);
  625. #define GC_NEXT(p) (*(GC_PTR *)(p))     /* Retrieve the next element    */
  626.                     /* in returned list.        */
  627.  
  628. #endif /* SOLARIS_THREADS */
  629.  
  630. /*
  631.  * If you are planning on putting
  632.  * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
  633.  * from the statically loaded program section.
  634.  * This circumvents a Solaris 2.X (X<=4) linker bug.
  635.  */
  636. #if defined(sparc) || defined(__sparc)
  637. #   define GC_INIT() { extern end, etext; \
  638.                GC_noop(&end, &etext); }
  639. #else
  640. #   define GC_INIT()
  641. #endif
  642.  
  643. #ifdef __cplusplus
  644.     }  /* end of extern "C" */
  645. #endif
  646.  
  647. #endif /* _GC_H */
  648.